/*
 * Created by Jaren at 2022/10/28 10:19
 */
import 'package:inmotion_protocol/base/base_factory.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/common/vehicle_info.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/le1/le1_settings.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/scooter_settings.dart';
import 'package:inmotion_protocol/lorin/factory/special/scooter/scooter_message_factory.dart';
import 'package:inmotion_protocol/lorin/lorin_message.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class LE1MessageFactory extends ScooterLorinSpecialMessageFactory<_LE1Cmd> implements SimpleBMSFeatureMsgFactoryTrait<LorinMessage> {
  LE1MessageFactory(ProductProperty productProperty) : super(productProperty);

  @override
  void initCompact(int apiVersion, VersionWrapper version) {
    compact = LE1CmdDefault();
  }

  LorinMessage genBrakeForceMsg(int type, int force) {
    return genSpecialOne2OneWriteMsg(compact!.brakeForceCmd(), ByteUtil.buildBytes(ByteUtil.num2BytesLe(type, 1), ByteUtil.num2BytesLe(force, 1)));
  }

  /// LE1骑行模式定义不同
  /// see [LE1Settings.toRidingMode]
  @override
  LorinMessage genSetRidingModeMsg(int mode) {
    // LE1行人模式标识为0
    if (mode == ScooterSettings.ridingModeAssist) mode = 0;
    return super.genSetRidingModeMsg(mode);
  }

  @override
  LorinMessage genRequestBMSCellsVoltageInfoMsg(int target) {
    return genSpecialOne2OneReadMsg(compact!.bmsCellsVoltageCmd());
  }
}

abstract class _LE1Cmd extends ScooterCmdCompact implements SimpleBMSFeatureCmdTrait {
  int brakeForceCmd();
}

class LE1CmdDefault extends _LE1Cmd {
  @override
  int bmsCellsVoltageCmd() {
    return 5;
  }

  @override
  int ridingModeCmd() {
    return 17;
  }

  @override
  int lockCmd() {
    return 18;
  }

  @override
  int brakeForceCmd() {
    return 16;
  }
}
